<?php 

require_once 'model/builder/AlipayTradePrecreateContentBuilder.php';
require_once 'service/AlipayTradeService.php';
$GLOBALS['alipay_config']=$config;

// print_r($config);die;
class Alipay{



	// 获取token
	function get_token($code,$adata){
		
		$aop = new AopClient ();

		$aop->appId = $adata['appid'];
		$aop->rsaPrivateKey =$adata['secret'];
		$aop->alipayrsaPublicKey=$adata['public'];

		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		$request = new AlipayOpenAuthTokenAppRequest();
		$biz=array(
			'grant_type'=>'authorization_code',
			'code'      =>$code,
			);
		$request->setBizContent(json_encode($biz));
		$result = $aop->execute ($request);
		return $result->alipay_open_auth_token_app_response;
	}



	// 预下单  仅支持花呗
	public function qrpay($outTradeNo,$subject,$totalAmount,$appAuthToken,$adata){

		$aop = new AopClient ();


		$aop->appId = $adata['appid'];
		$aop->rsaPrivateKey =$adata['secret'];
		$aop->alipayrsaPublicKey=$adata['public'];
		// $aop->appId = '2018040502507655';
		// $aop->rsaPrivateKey ='MIIEpAIBAAKCAQEA4NIM+i7C6ZM1H3oUhz/W6ZoJy4N67ql9SlSsnJU3odoMAnPqjJTIGUN5/r3UrGkAo3CUdUIko26pTHEd6EqJvGeeexJjtniO1mbZQ7t3912zAD4cCrgm9yYLbT92Eq2DTp091yI1zI9+FCyNBdrQaaz2s5u15lSv40fwknhtVLkgV68uPwM1gxIswFHWAtVKXtgkTfSAH89gPs0DeEZBgQMp5ddBRHnn35tEtyiZgLQ7nsG2k2IpkZHMlqXoAw1e5Dpgsfw6aSc2TzqQbL94B6vha7C5O2SfFYXCOZQm+Pb+6skNw5UParCuvkK4N3/O/f9Th/juqyFLyqBiPsuu9QIDAQABAoIBAQCOWXGOF32il2rTe2D0Ih/Z2YbKLX3xD7AdGHAS1Qhcmstv4iVozevE00yzV08Ju5KD6gyWEkNQmAjqNdVR96Augw19r68jIkKOzqBSmqjjH5jvVmQ7BU7mvlgN3FjOir7RUA6P4UF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
		// $aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApcv7aveVeZ0YdNOZDgmA35HafjfF+n2AHG341EdVJCLeqKpyR4O7OCnQAARW4sXWEnu3ZolzOJPAM/Zqj7jVz9bOVRjRolj9eyy5tcU/S2C+n7JOpDxY0J3M2jNfPbbsLOuMPVMzRftAtBsAdXtU7azP+sMQ+PjYMARx0PHVDUWQZDZPG7OyjwJONcYBPZgTu2MdW3l5RVA/3tVjbbkUSbFSRKpQxIie0eIRNdt9ba0l4kUGlM9Tl1ns52sCTbVky78szurzlAylPdNAVbL1AKOCqPibmWtSaCnL/aCrMLJkjvJI8ikxxUq2oUyDkVVABz3qlb4Ze5DJSf4OYI4ZxQIDAQAB';
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		//异步通知地址,只有扫码支付预下单可用
		// $aop->notifyUrl= "http://qnm.upslope.cn/return.php";
		// $aop->app_auth_token=$appAuthToken;
		$request = new AlipayTradePrecreateRequest();
		$biz=array(
			'out_trade_no'=>$outTradeNo,
			'total_amount'    =>$totalAmount,
			'subject'          =>$subject,
			'timeout_express'  =>'5m',
			'enable_pay_channels'=>'balance,pcredit,creditCard,creditCardExpress,creditCardCartoon',
			);
		$request->setBizContent(json_encode($biz));
		$request->setNotifyUrl("http://qnm.upslope.cn/return.php");
//		$request->setReturnUrl("http://qnm.upslope.cn/home/index/huabeiReturnUrl");
//		$request->setReturnUrl("http://qnm.upslope.cn/admin/test/test");
		$result = $aop->execute ($request,'',$appAuthToken);
		return $result->alipay_trade_precreate_response;

		
	}
	// 分期
		public function qrpay2($outTradeNo,$subject,$totalAmount,$appAuthToken,$extend_params,$adata){

		$aop = new AopClient ();

		$aop->appId = $adata['appid'];
		$aop->rsaPrivateKey =$adata['secret'];
		$aop->alipayrsaPublicKey=$adata['public'];
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		
		$request = new AlipayTradePrecreateRequest();
		/*
		"extend_params":{"hb_fq_num":"3","hb_fq_seller_percent":"100"}
		*/
		$biz=array(
			'out_trade_no'=>$outTradeNo,
			'total_amount'    =>$totalAmount,
			'subject'          =>$subject,
			'timeout_express'  =>'5m',
			'extend_params'    =>$extend_params,
			'enable_pay_channels'=>'pcreditpayInstallment',
			);
		$request->setBizContent(json_encode($biz));
		//异步通知地址,只有扫码支付预下单可用
		$request->setNotifyUrl("http://qnm.upslope.cn/return_pcreate.php");
		$result = $aop->execute ($request,'',$appAuthToken);
		return $result->alipay_trade_precreate_response;

		
	}






		// 交易结算  分账
		public function settle($out_request_no,$trade_no,$royalty_parameters,$app_auth_token,$adata){

			$aop = new AopClient ();

			$aop->appId = $adata['appid'];
			$aop->rsaPrivateKey =$adata['secret'];
			$aop->alipayrsaPublicKey=$adata['public'];
			$aop->apiVersion = '1.0';
			$aop->signType = 'RSA2';
			$aop->postCharset='UTF-8';
			$aop->format='json';
				$request = new AlipayTradeOrderSettleRequest ();
				// $royalty_parameters=json_encode($royalty_parameters);
				$biz=array(
					'royalty_parameters'=>[$royalty_parameters],
					'out_request_no'    =>$out_request_no,
					'trade_no'          =>$trade_no,
					);

				$request->setBizContent(json_encode($biz));
				// return $request;
				$result = $aop->execute ($request,'',$app_auth_token);
				return $result->alipay_trade_order_settle_response;

		}








	// 单笔交易查询  
	public function transorderquery($outBizNo,$adata){
	             // transfer3($out_biz_no,$alipay,$amount,$remark)

		$aop = new AopClient ();

		$aop->appId = '2018020602149590';
		$aop->rsaPrivateKey = 'MIIEpQIBAAKCAQEAum9NDqVACMkCTccZMC0TRqkXHVd/aP2OqmL51V9zOtQvR9QZn0eYpy8OjUn+Df+I7KZwClDbZAxrWZ3l6GsvCfUKjEHyG8cpHCvkAdsMJvh7mkmtb5j4SjVb+mdNou1rXhAU/V0YXYGsg5/qtI7Icr+6dFViHKLxY5rRwmkxaUZWvqg3LXqmqa/0LRME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9izMs6f28TlvDSNKn1ndU5ps65+LjTKKBlMML1OUcUHsU7we6KdyRoTqCgdhifYjbbMojU1upp3uA725y5uUKvIysM8qThGyJbxUH+dG7YFZqNNO4lKS6oc0qDNK7ukdAoGAeR14V9VpPmdqW6SvXK90WDQKWUakoSW8uwmlKCvydOiywpLePj2pCJ0PN7KpGPGbpdQrJ9UptE9Vd3OaYhLGPWf1FtFa9aCBJ1AqoS2LY9W501tdwUt/1Cu2ozEy9sDESEGpMW63bJe6Rs5mU7brhLxmOZ6NtP9daS8haYHSsX0=';
		$aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi1MgizoKbmqMXVragwjrpP8inxFFvSADduvecwML/ejcSBNODcdLgxKABJ6JC2FC5rQhSxSOzIJGGUeAdBffI7Ennc8cILcF3dcV1TDy3in24/pmJ2Nj/LhN7kjf6Yu6dCcE3xzgEHDCxvC8fGqGKr3N+6PhL3bifW8k2szMjXsBX0tqdW17dQrxYo8yeTaj86bijZZxrK+3daXL0eu/c/kXAPY6dHNt3iuK6D30Zx87M/Z4mnokHBNzmNspaOUkgTc+4EmcnnaJf3FLmiemabSICjbsCIWS4AqP8CPoqOZLsn2JrUqRIawL2rYsTRffq+1IKR6lh8iywFDIjtWAXwIDAQAB';
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		
		$request = new AlipayFundTransOrderQueryRequest ();
		$biz=array(
			'out_biz_no'         =>$outBizNo,
			);
		$request->setBizContent(json_encode($biz));

		$result = $aop->execute ( $request);
		return $result->alipay_fund_trans_order_query_response;

	}






	// 花呗+分期回款支付宝以及提现支付宝
	public function transfer5($outBizNo,$payeeAccount,$amount,$remark,$payer_show_name,$adata){

		$aop = new AopClient ();
		$aop->appId = $adata['tx_appid'];
		$aop->rsaPrivateKey =$adata['tx_secret'];
		$aop->alipayrsaPublicKey=$adata['tx_public'];

		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		
		$request = new AlipayFundTransToaccountTransferRequest();
		$biz=array(
			'out_biz_no'         =>$outBizNo,
			'payee_type'         =>'ALIPAY_USERID',
			'amount'             =>$amount,
			'payee_account'      =>"{$payeeAccount}",
			'payer_show_name'    =>$payer_show_name,
			'remark'             => $remark,
			);
		$request->setBizContent(json_encode($biz));
// return $request;
		$result = $aop->execute ( $request);
		return $result->alipay_fund_trans_toaccount_transfer_response;

	}


		// 花呗单笔交易查询  
	public function transorderquery2($outBizNo,$adata){
	             // transfer3($out_biz_no,$alipay,$amount,$remark)

		$aop = new AopClient ();

		$aop->appId = '2018012402057071';  //
		$aop->rsaPrivateKey = 'MIIEpQIBAAKCAQEAqiTLVv+ndx15rk1mwZOKhu4g4g4P1DcKE3Slkd/O3mGLi9Ofay9lQ1NN8kr2kX93tbLT+LXdxYVCg0LPHNdJl2khi9g2mgXZXdg0TEZ3mNyMEC2jxnuomIzwOZokiHjK5qGrpYypSKG9MSS/9ete2ypsGy7/gaY/RHjS9/tTLQMq9MWi+udO5feeN5ZMDoU0aDMr2eoax+R/k++q/9lue+v+1Rl2CV1us4GJE6QXJc2Tc5q7dn+OVAkbpbVgjN3QlUR7gSVF0nHAChqqLg7K4P40gwO2oIFuSQX7dmEWYwbWKWcz4M1GpRgusguPyRL04bUzmGYCllB528E/kRmIjQIDAQABAoIBACUUrchBIXNBVkKZldp8WEV94mbB38tFT4nb8zWujDxFEA+iSG0owC4DGsrCcahWGnXcCnx6zmF84HkQ92h2txArlmcO8R9yRORB+WviM7B56ICiIb5hNuZAWR6ErI+dm8LV2z3SQRaWQVBTjQ6AhITJYxQtkvvjiohWuMvY11DIs7198GNvwZ1q6BWjFZx+R/aKU5ZHfdaCWUZEUl7iMRvpbVj5v97g7h3UX2HM+ICwkiSne6aWZjYKRQaywMEdSWv8F+hwVqd2JrSa1dM1MT6WKAutN5BTuv2drt6PvsbVcusyrf8JhrbH0asGxKs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j/LhN7kjf6Yu6dCcE3xzgEHDCxvC8fGqGKr3N+6PhL3bifW8k2szMjXsBX0tqdW17dQrxYo8yeTaj86bijZZxrK+3daXL0eu/c/kXAPY6dHNt3iuK6D30Zx87M/Z4mnokHBNzmNspaOUkgTc+4EmcnnaJf3FLmiemabSICjbsCIWS4AqP8CPoqOZLsn2JrUqRIawL2rYsTRffq+1IKR6lh8iywFDIjtWAXwIDAQAB';
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		
		$request = new AlipayFundTransOrderQueryRequest ();
		$biz=array(
			'out_biz_no'         =>$outBizNo,
			);
		$request->setBizContent(json_encode($biz));

		$result = $aop->execute ( $request);
		return $result->alipay_fund_trans_order_query_response;

	}








	// 
	public function transfer8($outBizNo,$payeeAccount,$amount,$remark,$payee_real_name,$payer_show_name,$adata){

		$aop = new AopClient ();


		$aop->appId = $adata['tx_appid'];
		$aop->rsaPrivateKey =$adata['tx_secret'];
		$aop->alipayrsaPublicKey=$adata['tx_public'];
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';

		
		$request = new AlipayFundTransToaccountTransferRequest();

		$biz=array(
			'out_biz_no'         =>$outBizNo,
			'payee_type'         =>'ALIPAY_LOGONID',
			'amount'             =>$amount,
			'payee_account'      =>"{$payeeAccount}",
			'payee_real_name'    =>$payee_real_name,
			'payer_show_name'    =>$payer_show_name,
			'remark'             => $remark,
			);
		$request->setBizContent(json_encode($biz));
		$result = $aop->execute ( $request);
		return $result->alipay_fund_trans_toaccount_transfer_response;

	}


}



?>
